package com.sinoglobal.ningxia.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.orhanobut.logger.Logger;
import com.sinoglobal.networkconnections.ResultCallback;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.adapter.RecommendFramentAdapter;
import com.sinoglobal.ningxia.beans.ProgramaListBean;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;

import java.util.Iterator;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCUserAction;
import fm.jiecao.jcvideoplayer_lib.JCUserActionStandard;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by XINDZ on 2017/2/20.
 * 功能:推荐栏目fragment
 */

public class RecommendLanMuFragment extends Fragment {
    private ProgramaListBean.ListBean listBean;
    private List<ProgramaListBean.ListBean.LanmuBean> lanmuBeanList;
    private ListView lv_recommend;
    private RecommendFramentAdapter recommendAdapter;

    public static RecommendLanMuFragment newInstance(ProgramaListBean.ListBean listBean) {
        RecommendLanMuFragment recommendLanMuFragment = new RecommendLanMuFragment();
        recommendLanMuFragment.listBean = listBean;
        return recommendLanMuFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_recomment_lanmu, null);
        lv_recommend = (ListView) view.findViewById(R.id.lv_comment_lanmu);
        if(listBean!=null){
        lanmuBeanList = listBean.getLanmu();
        recommendAdapter = new RecommendFramentAdapter(getActivity(), lanmuBeanList);
        lv_recommend.setAdapter(recommendAdapter);
        JCVideoPlayer.setJcUserAction(new MyUserActionStandard());
        }
        return view;
    }

    class MyUserActionStandard implements JCUserActionStandard {

        @Override
        public void onEvent(int type, String url, int screen, Object... objects) {
            switch (type) {
                case JCUserAction.ON_CLICK_START_ICON:
                    Iterator<ProgramaListBean.ListBean.LanmuBean> iterator = lanmuBeanList.iterator();
                    while (iterator.hasNext()) {
                        ProgramaListBean.ListBean.LanmuBean lanmuBean = iterator.next();
                        String video_url = lanmuBean.getVideo_url();
                        if (video_url != null && video_url.equals(url)) {
                            lanmuBean.setSums(lanmuBean.getSums() + 1);
                            recommendAdapter.notifyDataSetChanged();
                            String id = lanmuBean.getId();
                            RemoteImpl.getInstance().getNewslookNum(this, id, 2).execute(new ResultCallback(getActivity()));
                        }
                    }
                    Logger.i("ON_CLICK_START_ICON" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_CLICK_START_ERROR:
                    Logger.i("ON_CLICK_START_ERROR" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_CLICK_START_AUTO_COMPLETE:
                    Logger.i("ON_CLICK_START_AUTO_COMPLETE" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_CLICK_PAUSE:
                    Logger.i("ON_CLICK_PAUSE" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_CLICK_RESUME:
                    Logger.i("ON_CLICK_RESUME" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_SEEK_POSITION:
                    Logger.i("ON_SEEK_POSITION" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_AUTO_COMPLETE:
                    Logger.i("ON_AUTO_COMPLETE" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_ENTER_FULLSCREEN:
                    Logger.i("ON_ENTER_FULLSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_QUIT_FULLSCREEN:
                    Logger.i("ON_QUIT_FULLSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_ENTER_TINYSCREEN:
                    Logger.i("ON_ENTER_TINYSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_QUIT_TINYSCREEN:
                    Logger.i("ON_QUIT_TINYSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_TOUCH_SCREEN_SEEK_VOLUME:
                    Logger.i("ON_TOUCH_SCREEN_SEEK_VOLUME" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserAction.ON_TOUCH_SCREEN_SEEK_POSITION:
                    Logger.i("ON_TOUCH_SCREEN_SEEK_POSITION" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;

                case JCUserActionStandard.ON_CLICK_START_THUMB:
                    Logger.i("ON_CLICK_START_THUMB" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                case JCUserActionStandard.ON_CLICK_BLANK:
                    Logger.i("ON_CLICK_BLANK" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
                    break;
                default:
                    Logger.i("unknow");
                    break;
            }
        }
    }
}
